package com.framerock.server;

import com.framerock.baselib.AA;
import com.framerock.baselib.CDate;
import com.framerock.baselib.CLog;
import com.framerock.baselib.Rock;

import java.util.Map;
import java.util.Scanner;

public class InputServer {

    private static boolean bool = true,startbool=false;

    /**
     * 输入命令的
     * */
    public static void start()
    {
        Scanner scanner = new Scanner(System.in);
        String help = "["+CDate.now()+"]hi,"+ AA.APPTYPE+",plase input...";
        help +="\nstart --开启主服务webserver和taskserver";
        help +="\ncreatedb --创建所有数据库";
        help +="\nupdatedb --更新所有数据库";
        help +="\nupdatedb num=模块编号 group=模块分组编号 --更新指定的模块";
        System.out.println(help);
        String action,input;
        while (bool) {
            input = scanner.nextLine(); // 获取一行输入
            if(!Rock.isEmpt(input)){
                CLog.print(input);
                if(input.equals("exit") || input.equals("stop")){
                    stop();
                    break;
                }
                action = decodeInput(input);
                if(action.equals("updatedb") || action.equals("createdb")){
                    MainServer.start(action, paramsMap);
                }
                if(action.equals("start") && !startbool){
                    startbool = true;
                    MainServer.start(action, paramsMap);
                }
            }
        }
        scanner.close();
        CLog.error("goodbye "+AA.APPTYPE+"");
    }

    private static Map<String, String> paramsMap = null;
    private static String decodeInput(String input)
    {
        String action = "",inputa[],arrs[],val;
        inputa      = input.split(" ");
        action      = inputa[0];
        paramsMap   = Rock.getMap();
        if(inputa.length > 1)for(int i=1;i<inputa.length;i++){
            arrs = inputa[i].split("=");
            val  = "";
            if(arrs.length>1)val = arrs[1];
            paramsMap.put(arrs[0], val);
        }
        return action;
    }

    public static void stop()
    {
        bool = false;
        if(startbool){
            HttpServer.stop();
            TaskServer.stop();
        }
    }
}
